package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    @RequestMapping("getAll")
    @ResponseBody
    public Map getAllGurus(){
        HashMap map = new HashMap();
        try {
            map.put("data", guruService.findAll());
            map.put("code", 0);
            return map;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("getOne")
    public Guru getGuruById(Guru guru){
        try {
            return guruService.findGuruByField(guru);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("addOne")
    @ResponseBody
    public Boolean addGuru(Guru guru) {
        try {
            guruService.insertGuru(guru);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delById")
    @ResponseBody
    public Boolean deleteGuruById(Guru guru) {
        try {
            guruService.removeGuruById(guru.getGuruId());
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delBulk")
    @ResponseBody
    public Boolean deleteMultiple(Integer[] ids) {
        try {
            guruService.removeMultiple(ids);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Boolean updateGuru(Guru guru) {
        try {
            guruService.renovateGuru(guru);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("addBulk")
    @ResponseBody
    public Boolean addGuruBulk(MultipartFile file) throws Exception {
        InputStream in = file.getInputStream();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        List<Guru> gurus = ExcelImportUtil.importExcel(in, Guru.class, params);
        gurus.stream().forEach(System.out::println);
        guruService.addGuruBulk(gurus);
        return true;
    }

    @RequestMapping("exportBulk")
    public void exportBulk(HttpServletResponse response){
        try{
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("guru.xlsx", "utf-8"));
            response.setContentType("application/vnd.ms-excel");
            List<Guru> all = guruService.findAll();
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师清单","guru"), Guru.class, all);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
